package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    /**
     * 处理列表请求
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQueryObject qo){
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page",page);

        return "region/list";
    }

    /**
     * 获取所有的目的地
     * @return
     */
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }

    /**
     * 新增或修改区域
     * @param region
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Region region){
        if(StringUtils.hasLength(region.getId())){
            regionService.update(region);
        }else{
            regionService.save(region);
        }
        return JsonResult.success();
    }

    /**
     * 根据区域id查询对应的目的地
     * @param rid
     * @return
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){

        //根据区域id查询所有的目的地
        List<Destination> list=destinationService.queryDestinationByRegionId(rid);
        return list;
    }

    /**
     * 修改热门状态
     * @param hot
     * @param id
     * @return
     */
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot,String id){

        //调用业务方法更改热门状态
        regionService.checkStatus( hot, id);
        return JsonResult.success();
    }

    /**
     * 删除区域
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        //调用业务方法更改热门状态
        regionService.delete(id);
        return JsonResult.success();
    }

    /**
     * 根据目的地级别查询目的地
     * @param deep
     * @return
     */
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        //查询指定目的地,省级城市
        List<Destination> list=regionService.queryByDeep(deep);
        return list;
    }
}
